package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Menu> menuList = menuService.findAll();
        if (menuList!=null&&menuList.size()>0){
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        }else {
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    @RequestMapping("/findAllLevel1")
    public Result findAllLevel1(){
        List<Menu> menuLevel1 = menuService.findAllLevel1();
        if (menuLevel1!=null&&menuLevel1.size()>0){
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuLevel1);
        }else {
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Menu menu = null;
        try {
            menu = menuService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
        } catch (RuntimeException e) {//抓取当前检查项和检查组有关联的异常信息
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    @RequestMapping("/findByUsername")
    public Result findByUsername(){
        //当spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象中
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user!=null){

            String username = user.getUsername();
            List<Menu> list =menuService.findByUsername(username);

//            System.out.println(list);

            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        }else {
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
}
